<extend name="Common/common" />
<block name="main">
    <div id="page-wrapper" class="gray-bg dashbard-1">
        <include file="Common/nav-header"/> <!--顶部-->
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>修改密码</h5>
                </div>
                <div class="ibox-content">
                    <form class="form-horizontal m-t " 
                        method="post" action="{:U('Index/changePassword')}">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">原始密码：</label>
                            <div class="col-sm-3">
                                <input  type="text" id="old_password" 
                                     class="form-control"  name="old_password">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">新密码：</label>
                            <div class="col-sm-3">
                                <input  type="password" id="new_password"  
                                       class="form-control" name="new_password">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">确认密码：</label>
                            <div class="col-sm-3">
                                <input  type="password" id="new_password2"  
                                       class="form-control" name="new_password2">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-3">
                                <button class="btn btn-primary" type="submit">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <!--尾部-->
        <include file="Common/footer"/>
    </div>

    <script>
        $('form').submit(function(){
            var old_password  = $('#old_password').val();	//获取原始密码
            var new_password  = $('#new_password').val(); 	//获取新密码
            var new_password2 = $('#new_password2').val();	//获取确认密码
            if(new_password == ''){		//新密码为空
                layer.msg('原始密码密码不能为空');
                return false;
            }
            if(new_password === old_password){	//新密码与原始密码相同
                layer.msg('新密码与原始密码不能相同');
                return false;
            }
            if(new_password !== new_password2){	//新密码与确认密码不一致
                layer.msg('新密码与确认密码不一致');
                return false;
            }
            var url  = $(this).attr('action');  //Index控制器下的changePassword方法
            $.ajax({
                type:"post", 	//提交方式
                url :url, 		//提交地址 
                data:{old_password:old_password,new_password:new_password},	//传递参数
                success: function(res){		//回调函数
                    if(res.status){			//修改成功
                        layer.msg(res.message,{time:1000},function(){
                            window.location.href = "{:U('Index/index')}";
                        });
                    }else{					//修改失败
                        layer.msg(res.message,{time:2000});
                    }
                }
            });
            return false;
        });
    </script>
</block>
